//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "FdmSolverDesc.h"
using namespace Cephei::QL::Methods::Finitedifferences::Solvers;
#undef HANDLE
#undef ABSTRACT
#define STRUCT
Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSolverDesc::CFdmSolverDesc (boost::shared_ptr<QuantLib::FdmSolverDesc>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmSolverDesc = NULL;
#endif
	_ppFdmSolverDesc = &childNative;
    
}
Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSolverDesc::CFdmSolverDesc (QuantLib::FdmSolverDesc& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phFdmSolverDesc = NULL;
#endif
	_ppFdmSolverDesc = new boost::shared_ptr<QuantLib::FdmSolverDesc> (&childNative);
    
    _FdmSolverDescOwner = owner;
    
}

Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSolverDesc::CFdmSolverDesc (CFdmSolverDesc^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmSolverDesc = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFdmSolverDesc = new boost::shared_ptr<QuantLib::FdmSolverDesc> (copy->GetShared());
        
    }
}
Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSolverDesc::CFdmSolverDesc (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmSolverDesc = NULL;
#endif
	if (!t->IsSubclassOf(CFdmSolverDesc::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSolverDesc::CFdmSolverDesc (QuantLib::Handle<QuantLib::FdmSolverDesc>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFdmSolverDesc = &childNative;
	_ppFdmSolverDesc = &static_cast<boost::shared_ptr<QuantLib::FdmSolverDesc>>(childNative.currentLink());
    
    _FdmSolverDescOwner = owner;
}
Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSolverDesc::CFdmSolverDesc (QuantLib::Handle<QuantLib::FdmSolverDesc> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFdmSolverDesc = &childNative;
	_ppFdmSolverDesc = &static_cast<boost::shared_ptr<QuantLib::FdmSolverDesc>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSolverDesc::CFdmSolverDesc (QuantLib::FdmSolverDesc childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmSolverDesc = NULL;
#endif
	_ppFdmSolverDesc = new boost::shared_ptr<QuantLib::FdmSolverDesc> (new QuantLib::FdmSolverDesc (childNative));
    
}
#endif

Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSolverDesc::~CFdmSolverDesc ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppFdmSolverDesc != NULL)
    {
	    delete _ppFdmSolverDesc;
        _ppFdmSolverDesc = NULL;
    }
}
Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSolverDesc::!CFdmSolverDesc ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppFdmSolverDesc != NULL)
    {
	    delete _ppFdmSolverDesc;
    }
}
QuantLib::FdmSolverDesc& Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSolverDesc::GetReference ()
{
    if (_ppFdmSolverDesc == NULL) throw REFNEW NativeNullException ();
	return **_ppFdmSolverDesc;
}
boost::shared_ptr<QuantLib::FdmSolverDesc>& Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSolverDesc::GetShared ()
{
    if (_ppFdmSolverDesc == NULL) throw REFNEW NativeNullException ();
	return *_ppFdmSolverDesc;
}
QuantLib::FdmSolverDesc* Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSolverDesc::GetPointer ()
{
    if (_ppFdmSolverDesc == NULL) throw REFNEW NativeNullException ();
	return &**_ppFdmSolverDesc;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FdmSolverDesc>& Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSolverDesc::GetHandle ()
{
	if (_phFdmSolverDesc == NULL)
	{
		_phFdmSolverDesc = new Handle<QuantLib::FdmSolverDesc> (*_ppFdmSolverDesc);
	}
	return *_phFdmSolverDesc;
}
#endif
bool Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSolverDesc::HasNative () 
{
	return (_ppFdmSolverDesc != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

